package com.sc.servlet.user.field;

import com.alibaba.fastjson.JSONObject;
import com.sc.pojo.BookField;
import com.sc.services.FieldServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.ParseException;

/**
 * @description: 退场
 * @author: sc
 * @create: 2023-01-07 19:22
 **/
@WebServlet("/quitField")
public class quitFieldServlet extends HttpServlet {

    private FieldServices fieldServices = new FieldServices();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String readLine = reader.readLine();
        System.out.println(readLine);
        JSONObject jsonObject = JSONObject.parseObject(readLine);
        BookField bookField = jsonObject.toJavaObject(BookField.class);
        String res=null;

        try {
            if(fieldServices.isTheRightTime(bookField)){
                res = "upd_suc";
                fieldServices.reviseFieldState1(bookField);
            }else {
                res = "wrong_time";
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(res);



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}